package net.aib.moneytracker;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class MoneyTracker extends Activity {
	private static final String TAG = "MoneyTracker";
	private static final int OLD_BUYS = 0;
	private static final int CATEGORIES_LIST = 1;
	private static final int COMPANIES_LIST = 2;
	
	private MoneyTrackerController mMTController;
	private TextView tAccount;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        mMTController = new MoneyTrackerController(this);
        
        displayCurrentAccount();
        buttons();
    }
    
    public void displayCurrentAccount(){
    	double account = mMTController.getCurrentAccount();
    	tAccount = (TextView) findViewById(R.layout.account);
    	tAccount.setText(Double.toString(account));
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	MenuInflater inflater = getMenuInflater();
    	inflater.inflate(R.menu.menu, menu);
    	return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	switch (item.getItemId()) {
		case R.id.addToAccount:
			addAmount();
			Log.d(TAG, "Add To Account Menu Button!");
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    	super.onActivityResult(requestCode, resultCode, data);
    	if(requestCode == OLD_BUYS)
    	{
    		if(resultCode == RESULT_OK)
    		{
				displayCurrentAccount();
    		}
    	}
    }
    
    public void addAmount(){
    	AlertDialog.Builder builder;
    	AlertDialog amountDialog;
    	
    	LayoutInflater inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
    	View layout = inflater.inflate(R.layout.addtoaccountdialog, null);
    	builder = new AlertDialog.Builder(this);
    	final EditText eAmount = (EditText) layout.findViewById(R.id.amount);
    	
    	builder.setView(layout)
    		   .setTitle(R.string.add_to_account)
    		   .setPositiveButton(R.string.update, new OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					Log.d(TAG,"Amount = " + eAmount.getText());
					mMTController.addToAccount(Double.parseDouble(eAmount.getText().toString()));
					displayCurrentAccount();
					dialog.dismiss();
				} 
				})
			   .setNegativeButton(R.string.cancel, new OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
				}
			});
    	amountDialog = builder.create();
    	amountDialog.show();
    }
    
    public void buttons(){
    	
    	final Button bAddBuy = (Button)findViewById(R.id.addBuy);
    	
    	// Fix because of the dialog.OnClickListener() import.
    	bAddBuy.setOnClickListener(new android.view.View.OnClickListener() {
			
			public void onClick(View v) {
				newBuy();
			}
		});
    	
    	final Button bAddCategory = (Button)findViewById(R.id.addCategory);
    	
    	// Fix because of the dialog.OnClickListener() import.
    	bAddCategory.setOnClickListener(new android.view.View.OnClickListener() {
			
			public void onClick(View v) {
				newCategory();
			}
		});
    	
    	final Button bAddCompany = (Button)findViewById(R.id.addCompany);
    	
    	// Fix because of the dialog.OnClickListener() import.
    	bAddCompany.setOnClickListener(new android.view.View.OnClickListener() {
			
			public void onClick(View v) {
				newCompany();
			}
		});
    	
    	final Button bViewOldBuys = (Button)findViewById(R.id.viewOldBuys);
    	
    	// Fix because of the dialog.OnClickListener() import.
    	bViewOldBuys.setOnClickListener(new android.view.View.OnClickListener() {
			
			public void onClick(View v) {
				viewOldBuys();
			}
		});
    	
    	
    	final Button bViewCategoriesList = (Button)findViewById(R.id.viewCategoriesList);
    	
    	// Fix because of the dialog.OnClickListener() import.
    	bViewCategoriesList.setOnClickListener(new android.view.View.OnClickListener() {
			
			public void onClick(View v) {
				viewCategoriesList();
			}
		});
    	
    	final Button bViewCompaniesList = (Button)findViewById(R.id.viewCompaniesList);
    	
    	// Fix because of the dialog.OnClickListener() import.
    	bViewCompaniesList.setOnClickListener(new android.view.View.OnClickListener() {
			
			public void onClick(View v) {
				viewCompaniesList();
			}
		});
    }
    
    public void viewOldBuys(){
        Intent oldBuys = new Intent(this, OldBuys.class);
        startActivityForResult(oldBuys, OLD_BUYS);
    }
    
    public void viewCategoriesList(){
        Intent categoriesList = new Intent(this, CategoriesList.class);
        startActivityForResult(categoriesList, CATEGORIES_LIST);
    }
    
    public void viewCompaniesList(){
        Intent companiesList = new Intent(this, CompaniesList.class);
        startActivityForResult(companiesList, COMPANIES_LIST);
    }
    
    public void newBuy(){
    	AlertDialog.Builder builder;
    	AlertDialog buyDialog;
    	
    	LayoutInflater inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
    	View layout = inflater.inflate(R.layout.newbuy, null);
    	builder = new AlertDialog.Builder(this);
    	final EditText eBuy = (EditText) layout.findViewById(R.id.newBuy);
    	final Spinner sCategory = (Spinner) layout.findViewById(R.id.category_spinner);
    	final Spinner sCompany = (Spinner) layout.findViewById(R.id.company_spinner);
    	
    	Cursor cCategory = mMTController.getGroups();
    	startManagingCursor(cCategory);
    	
        SimpleCursorAdapter aCategory = new SimpleCursorAdapter(
                this,
                android.R.layout.simple_spinner_item,
                cCategory, 
                new String[] {"name"},  
                new int[] {android.R.id.text1});

        aCategory.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        sCategory.setAdapter(aCategory);
        
    	Cursor cCompany = mMTController.getCompanies();
    	startManagingCursor(cCompany);
    	
        SimpleCursorAdapter aCompany = new SimpleCursorAdapter(
                this,
                android.R.layout.simple_spinner_item,
                cCompany, 
                new String[] {"name"},  
                new int[] {android.R.id.text1});

        aCompany.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        sCompany.setAdapter(aCompany);
    	
    	builder.setView(layout)
    		   .setTitle(R.string.new_buy)
    		   .setPositiveButton(R.string.add, new OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					if(eBuy.getText().length() > 0){
						Log.d(TAG,"New buy amount = " + eBuy.getText());
						Log.d(TAG,"Category selected = " + sCategory.getSelectedItemId());
						Log.d(TAG,"Company selected = " + sCompany.getSelectedItemId());
						mMTController.newBuy(Double.parseDouble(eBuy.getText().toString()),
								sCategory.getSelectedItemId(), 
								sCompany.getSelectedItemId());
						displayCurrentAccount();
						dialog.dismiss();
					}
					else{
						Toast.makeText(getApplicationContext(), "Vantar texta", Toast.LENGTH_LONG).show();
					}
				} 
				})
			   .setNegativeButton(R.string.cancel, new OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
				}
			});
    	buyDialog = builder.create();
    	buyDialog.show();
    }
    
    public void newCategory(){
    	AlertDialog.Builder builder;
    	AlertDialog categoryDialog;
    	
    	LayoutInflater inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
    	View layout = inflater.inflate(R.layout.addnewcategory, null);
    	builder = new AlertDialog.Builder(this);
    	final EditText eGroup = (EditText) layout.findViewById(R.id.group_name);
    	
    	builder.setView(layout)
    		   .setTitle(R.string.new_group)
    		   .setPositiveButton(R.string.add, new OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					if(eGroup.getText().length() > 0){
						Log.d(TAG,"Group Name = " + eGroup.getText());
						mMTController.addNewGroup(eGroup.getText().toString());
						dialog.dismiss();
					}
					else{
						Toast.makeText(getApplicationContext(), "Vantar texta", Toast.LENGTH_LONG).show();
					}
				} 
				})
			   .setNegativeButton(R.string.cancel, new OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
				}
			});
    	categoryDialog = builder.create();
    	categoryDialog.show();
    }
    
    public void newCompany(){
    	AlertDialog.Builder builder;
    	AlertDialog companyDialog;
    	
    	LayoutInflater inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
    	View layout = inflater.inflate(R.layout.addnewcompany, null);
    	builder = new AlertDialog.Builder(this);
    	final EditText eCompany = (EditText) layout.findViewById(R.id.company_name);
    	
    	builder.setView(layout)
    		   .setTitle(R.string.new_company)
    		   .setPositiveButton(R.string.add, new OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					if(eCompany.getText().length() > 0){
						Log.d(TAG,"Company Name = " + eCompany.getText());
						mMTController.addNewCompany(eCompany.getText().toString());
						dialog.dismiss();
					}
					else{
						Toast.makeText(getApplicationContext(), "Vantar texta", Toast.LENGTH_LONG).show();
					}
				} 
				})
			   .setNegativeButton(R.string.cancel, new OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
				}
			});
    	companyDialog = builder.create();
    	companyDialog.show();
    }
}